using System;
using QFramework;
using UnityEngine;
using static GiftManager;

public class LikeSocket : MonoBehaviour
{
    private StudentManager studentManager;
    private void Start()
    {
        studentManager = FindObjectOfType<StudentManager>();
        // Subscribe to the OnLiveLikeReceived event
        ReceiveSocket receiveSocket = FindObjectOfType<ReceiveSocket>();
        if (receiveSocket != null)
        {
            receiveSocket.OnLiveLikeReceived += HandleLiveLikeReceived;
        }
    }

    private void HandleLiveLikeReceived(LiveLikeData likeData)
    {
        // Handle the like event here
        Debug.Log($"处理点赞: {likeData.nickname} 点赞了 {likeData.like_num} 次");
        if(studentManager == null){
            studentManager = FindObjectOfType<StudentManager>();
        }
        // You can add additional logic here, such as updating UI or triggering animations
        if(!studentManager.ContainsStudent(likeData.game_user.id.ToString())){
            Debug.Log($"用户{likeData.game_user.id}不存在，改为让用户进场");
            studentManager.UserEnter(new LiveCommentData{
                game_user = likeData.game_user,
                nickname = likeData.nickname,
                avatar_url = likeData.avatar_url
            });
        }
        Debug.Log($"用户{likeData.game_user.id}存在");
        GameObject student = studentManager.GetStudentById(likeData.game_user.id.ToString());
        StringEventSystem.Global.Send(EventID.PlayGiftAnimation, new object[]{AnimaName.DianZan,student});
        StringEventSystem.Global.Send(EventID.DianZan, likeData.like_num);
    }

    private void OnDestroy()
    {
        // Unsubscribe from the event to prevent memory leaks
        ReceiveSocket receiveSocket = FindObjectOfType<ReceiveSocket>();
        if (receiveSocket != null)
        {
            receiveSocket.OnLiveLikeReceived -= HandleLiveLikeReceived;
        }
    }
}